package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.StoreMapper;
import com.zcf.pojo.Riding;
import com.zcf.pojo.Store;
import com.zcf.service.StoreService;
import com.zcf.utils.UploadImgUtils;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/store")
@CrossOrigin
@ResponseBody
public class StoreController {
	@Autowired
	private StoreService storeService;
	@Autowired
	private StoreMapper storeMapper;

	// 商家认证
	@PostMapping("/changeStore")
	public Body changeStore(String userId, String name, String range, String address, String zheng, String fan,
			String longitude, String latitude, String phone, String notice,String fl) {
		return storeService.changeStore(userId, name, range, address, zheng, fan, longitude, latitude, phone, notice,fl);
	}

	// 上传正面照
	@PostMapping("/storeZheng")
	public Body storeZheng(MultipartFile[] zurl) {
		if (zurl != null && !"".equals(zurl)) {
			try {
				String zheng = UploadImgUtils.uploadFiles(zurl);
				return Body.newInstance(zheng);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Body.newInstance(501, "照片不能为空");
	}

	// 上传反面照
	@PostMapping("/storeFan")
	public Body storeFan(MultipartFile[] furl) {
		if (furl != null && !"".equals(furl)) {
			try {
				String fan = UploadImgUtils.uploadFiles(furl);
				return Body.newInstance(fan);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return Body.newInstance(501, "照片不能为空");
	}

	// 商家列表
	@PostMapping("/checkList")
	public Body checkList(Integer status) {
		List<Store> list = storeMapper.checkList(status);
		if (!list.isEmpty()) {
			return Body.newInstance(list);
		}
		return Body.newInstance(501, "未有验证信息");
	}

	// 商家通过
	@PostMapping("/changeStatus")
	public Body changeStatus(String id) throws Exception {
		return storeService.changeStatus(id);
	}

	/**
	 * 拒绝通过
	 */
	@PostMapping("refuseStatus")
	public Body refuseStatus(String id, String reason) {
		return storeService.refuseStatus(id, reason);
	}

	/**
	 * 查询未审核
	 */
	@PostMapping("queryCheckList")
	public Body queryCheckList(String time) {
		EntityWrapper<Store> ew = new EntityWrapper<>();
		if (time != null) {
			ew.like("time", time);
		}
		ew.eq("status", 0);
		List<Store> list = storeMapper.selectList(ew);
		if (list.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.BODY_500;
	}

	/**
	 * 查询单个商家
	 */
	@PostMapping("addStoreOne")
	public Body addStoreOne(String id) {
		Store store = new Store();
		store.setId(id);
		store = store.selectById();
		if (store != null) {
			return Body.newInstance(store);
		}
		return Body.BODY_500;
	}
}
